//
//  oldFamilyChooseParentListTableViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/4/12.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilyChooseParentListTableViewController.h"
#import "oldFamilyFirstTableViewCell.h"
#import "oldHistoryActivityViewController.h"

@interface oldFamilyChooseParentListTableViewController ()
@property (nonatomic, copy)NSArray *familyArray;
@end

static NSString *const identifier = @"oldFamilyFirstTableViewCell";

@implementation oldFamilyChooseParentListTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    [self rigisterCell:identifier];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"选择亲属";
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetRelationList params:nil success:^(id data) {
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        [weakSelf.tableView.mj_header endRefreshing];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
        [weakSelf.tableView.mj_header endRefreshing];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [NSMutableArray array];
    for (int i = 0; i < [data count]; i++) {
        oldServer *old = [[oldServer alloc] initWithDictionary:data[i]];
        [array addObject:old];
    }
    
    _familyArray = array;
}

#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [_familyArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [self configFristSection:tableView indexPath:indexPath];
}


- (UITableViewCell *)configFristSection:(UITableView *)tableView indexPath:(NSIndexPath *)indexPath
{
    oldFamilyFirstTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldFamilyFirstTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.server = _familyArray[indexPath.row];
    cell.ifShowEdit = NO;
    
    return cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 105;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    oldHistoryActivityViewController *vc = [oldHistoryActivityViewController new];
    oldServer *old = _familyArray[indexPath.row];
    vc.parentsId = old.uid;
    [self.navigationController pushViewController:vc animated:YES];
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}


@end
